package com.bsoft.webflux.controller;

import com.bsoft.webflux.pojo.UserVO;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.server.*;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

/**
 * @author: hdh
 * @Desc:
 * @create: 2024-03-13 10:44
 **/
@Configuration
public class UserRouter {
    @Bean
    public RouterFunction<ServerResponse> userListRouterFunction(){
        return route(GET("/user2/list"), new HandlerFunction<ServerResponse>() {
            @Override
            public Mono<ServerResponse> handle(ServerRequest serverRequest) {
                List<UserVO> result = new ArrayList<>();
                result.add(new UserVO(1,"yudaoyuanma"));
                result.add(new UserVO(2,"woshiyutou"));
                result.add(new UserVO(3,"hdh"));
                return ok().bodyValue(result);
            }
        });
    }


    @Bean
    public  RouterFunction<ServerResponse> userGetRouterFunction(){
        return  route(GET("/users2/get"),
                new HandlerFunction<ServerResponse>() {
                    @Override
                    public Mono<ServerResponse> handle(ServerRequest serverRequest) {
                        //获得编号
                        Integer id = serverRequest.queryParam("id").map(s -> StringUtils.isEmpty(s) ?null: Integer.valueOf(s)).get();
                        //查询用户
                        UserVO  userVO = new UserVO(id, UUID.randomUUID().toString());
                        //返回列表
                        return  ok().bodyValue(userVO);
                    }
                }
        );
    }


    @Bean
    public  RouterFunction<ServerResponse> demoRouterFunction(){
        return route(GET("/user2/demo"),request->ok().bodyValue("demo"));
    }
}
